package com.hmall.user.lanjieqi;


import com.alibaba.nacos.common.utils.StringUtils;
import com.hmall.user.Utils.ThreadLocalUtil;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class UserLanJieQi implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
       /* //获取传过来的请求头中的值
        String key = request.getHeader("authorization");
        System.out.println(key);
        ThreadLocalUtil.setUserId(Long.parseLong(key));
        return HandlerInterceptor.super.preHandle(request, response, handler);*/
        String authorization = request.getHeader("authorization");
        if (StringUtils.isNotBlank(authorization)){
            long userId = Long.parseLong(authorization);
            ThreadLocalUtil.setUserId(userId);
        }
        return true;

    }

    /**
     * 开始的时候就拦截
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */



    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }


}
